﻿using Entity;
using IDal;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dal
{
    /// <summary>
    /// 所有Dal的父类
    /// </summary>
    public class BaseDal<T> : IBaseDal<T> where T : BaseEntity
    {
        DecorationDbContext _dbContext;

        public BaseDal(DecorationDbContext dbContext)
        {
            _dbContext = dbContext;
        }

      
        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        public bool CreateEntity(T entity)
        {
            //_dbContext.DepartmentInfos.Add(entity);
            _dbContext.Set<T>().Add(entity);
            int index = _dbContext.SaveChanges();
            if (index > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 真删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool DeleteEntity(T entity) 
        {
            _dbContext.Set<T>().Remove(entity);
            int index = _dbContext.SaveChanges();
            if (index > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 批量真删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool BatchDeleteEntity(List<T> entity)
        {
            _dbContext.Set<T>().RemoveRange(entity);
            int index = _dbContext.SaveChanges();
            if (index > 0)
            {
                return true;
            }
            return false;
        }


        /// <summary>
        /// 通过id真删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteEntityById(string id) 
        {
            var entity = _dbContext.Set<T>().FirstOrDefault(u => u.Id == id);
            if (entity != null)
            {
                _dbContext.Set<T>().Remove(entity);

                int index = _dbContext.SaveChanges();
                if (index > 0)
                {
                    return true;
                }
                return false;
            }
            else
            {
                return false;
            }
        }


        ///// <summary>
        /// 获取数据集
        /// </summary>
        public DbSet<T> GetDbSet() 
        {
            return _dbContext.Set<T>();
        }


        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateEntity(T entity) 
        {
            _dbContext.Set<T>().Update(entity);
            int index = _dbContext.SaveChanges();
            if (index > 0)
            {
                return true;
            }
            return false;
        }

    }
}
